/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.http.client.domain.property;

import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * OkHttp相关属性.
 *
 * @author zengdegui
 * @since 2018/10/10
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "http.client")
public class HttpClientProperty {

    /**
     * 是否启用.
     */
    @Builder.Default
    private Boolean enabled = Boolean.TRUE;

    /**
     * 连接保持时间。如果超过这个时间没有任务调度，则会被回收（单位：秒）.
     */
    @Builder.Default
    private Duration connectionKeepAlive = Duration.ofSeconds(300);

    /**
     * 从连接池中获取到连接的最长时间（单位：秒）.
     */
    @Builder.Default
    private Duration connectionRequestTimeout = Duration.ofSeconds(300);

    /**
     * 响应超时时间（单位：秒）.
     */
    @Builder.Default
    private Duration responseTimeout = Duration.ofSeconds(60);

    /**
     * 是否启用压缩.
     */
    @Builder.Default
    private Boolean enabledCompression = Boolean.TRUE;

    /**
     * 是否开启重定向.
     */
    @Builder.Default
    private Boolean redirectsEnabled = true;

    /**
     * 重定向的最大次数.
     */
    @Builder.Default
    private Integer maxRedirects = 5;

    /**
     * 是否启用观察.
     */
    @Builder.Default
    private Boolean enabledObservation = Boolean.TRUE;

    /**
     * 线程池相关属性.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private Pool pool = new Pool();

    /**
     * 代理相关属性.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private Proxy proxy = new Proxy();

    /**
     * 重试相关属性.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private Retry retry = new Retry();

    /**
     * 线程池相关属性.
     *
     * @author zengdegui
     * @since 2023/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Pool {

        /**
         * 最大连接数.
         */
        @Builder.Default
        private Integer maxConnTotal = 25;

        /**
         * 同路由并发数.
         */
        @Builder.Default
        private Integer maxConnPerRoute = 5;
    }

    /**
     * 代理相关属性.
     *
     * @author zengdegui
     * @since 2023/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Proxy {

        /**
         * 是否开启代理.
         */
        @Builder.Default
        private Boolean enabled = Boolean.FALSE;

        /**
         * 主机.
         */
        private String host;

        /**
         * 端口.
         */
        private Integer port;

        /**
         * 用户名.
         */
        private String username;

        /**
         * 密码.
         */
        private String password;

    }

    /**
     * 重试相关属性.
     *
     * @author zengdegui
     * @since 2023/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Retry {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 最大重试次数.
         */
        @Builder.Default
        private Integer maxAttempts = 3;

        /**
         * 重试间隔（单位：秒）.
         */
        @Builder.Default
        private Duration period = Duration.ofSeconds(1);

    }
}
